/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JColorChooser;
import javax.swing.JComboBox;

import who.tmap.services.ColorService;

public class JComboBoxColor extends JComboBox {
	
	private ColorService colorService =new ColorService();
	private ItemListener itemStateChanged;
	private boolean showColorChooserIfNecessary;

	public JComboBoxColor() {
		super( new ColorService().getComboBoxIntArray() );
		initialize();
	}
	
	
	/**
	 * To control when the custom color selection should appear
	 * @param enable
	 */
	public void enableItemStateChanged( boolean enable ) {
		if ( enable ) {
			addItemListener( itemStateChanged );
		} else {
			removeItemListener( itemStateChanged );
		}
	}
	
	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		setRenderer( new JComboBoxColorRenderer( colorService ) );
		
		setSelectedIndex( ColorService.DEFAULT_COLOR_IDX );		

		itemStateChanged =new ItemListener() {
        	public void itemStateChanged(ItemEvent e) {
        		if (e.getStateChange() == ItemEvent.SELECTED ) {
	        		// if custom color is selected, opens the color chooser dialog
					if ( getSelectedIndex() == ColorService.CUSTOM_COLOR_IDX && showColorChooserIfNecessary ) {
						
						Color selectedColor =JColorChooser.showDialog( null, "Select a color", 
																	   colorService.getColors()[ ColorService.CUSTOM_COLOR_IDX ] );
						
						if ( selectedColor != null ) {
							setSelectedColor( selectedColor );
						}
					}   
        		}
        	}
        };	
        
        showColorChooserIfNecessary =true;

        enableItemStateChanged( true );
	}	
	
	public Color getSelectedColor() {
		return colorService.getColors()[ getSelectedIndex() ]; 
	}
	
	public void setSelectedColor( Color color, boolean showColorChooserIfNecessary ) {
		int colorIdx =colorService.getIndexForColor( color );
		if ( colorIdx == ColorService.CUSTOM_COLOR_IDX ) {
			colorService.setCustomColor( color );
		}
		
		boolean saveShow =this.showColorChooserIfNecessary;
		this.showColorChooserIfNecessary =showColorChooserIfNecessary;
		
		setSelectedIndex( colorIdx );
		
		this.showColorChooserIfNecessary =saveShow;
	}
	
	public void setSelectedColor( Color color ) {
		setSelectedColor( color, true );
	}
	
	public ColorService getColorService() {
		return colorService;
	}
	
	public void setColorService( ColorService colorService ) {
		this.colorService =colorService;
	}
	
}
